class SyncBailHook {
    events = [];

    constructor() {
        console.log('SyncBailHook');
    }

    tap(name, cb) {
        cb = typeof cb === 'function' ? cb : (...args) => args;
        this.events.push(cb);
    }

    call(...args) {
        // 1. 用下标实现
        // const events = this.events;
        // let index = 0;
        // while (events[index] && events[index](args) === undefined && index < events.length) {
        //     index++;
        // }

        // 2. 用Symbol.iterator方法实现
        const iterator = this.events[Symbol.iterator]();
        let res = iterator.next();
        while(res.value && res.value(args) === undefined) {
            res = iterator.next();
        }
    }
}

module.exports = SyncBailHook;